package com.kerwin.springcloud.controller;

import cn.hutool.core.lang.UUID;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.kerwin.springcloud.entities.CommonResult;
import com.kerwin.springcloud.entities.Payment;
import com.kerwin.springcloud.handler.CustomerBlockHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class RateLimitController {

    @GetMapping(value = "/bySource")
    @SentinelResource(value = "bySource", blockHandler = "bySourceException")
    public CommonResult bySource(){
        return new CommonResult(200, "按资源名称限流测试OK", new Payment(1l, "serial001"));
    }

    public CommonResult bySourceException(BlockException exception){
        return new CommonResult(444, exception.getClass().getCanonicalName());
    }

    @GetMapping(value = "/byUrl")
    public CommonResult byUrl(){
        return new CommonResult(200, "按URL限流测试OK", new Payment(1l, "serial002"));
    }

    @GetMapping(value = "/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class, blockHandler = "blockException2")
    public CommonResult customerBlockHandler(){
        return new CommonResult(200, "客户自定义", new Payment(1l, "serial003"));
    }
}
